package sharetime.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	private static final int CALENDAR_PERIODS[] = { 1, 2, 5, 11, 12, 13, 14 };

	public static Calendar getCalendarDay(int year, int month, int day) {
		return initCalendar(year, month, day, 0, 0, 0, 0);
	}

	public static java.util.Date getDateDay(int year, int month, int day) {
		return getCalendarDay(year, month, day).getTime();
	}

	public static java.util.Date getSqlDateDay(int year, int month, int day) {
		return new Date(getCalendarDay(year, month, day).getTimeInMillis());
	}

	public static Calendar toEndOfPeriod(Calendar calendar, int period) {
		boolean zero = false;
		for (int i = 0; i < CALENDAR_PERIODS.length; i++) {
			if (zero)
				calendar.set(CALENDAR_PERIODS[i], calendar
						.getMaximum(CALENDAR_PERIODS[i]));
			if (CALENDAR_PERIODS[i] == period)
				zero = true;
		}

		if (!zero)
			throw new IllegalArgumentException("unknown Calendar period: "
					+ period);
		else
			return calendar;
	}

	public static Calendar toStartOfPeriod(Calendar calendar, int period) {
		boolean zero = false;
		for (int i = 0; i < CALENDAR_PERIODS.length; i++) {
			if (zero)
				if (CALENDAR_PERIODS[i] == 5)
					calendar.set(5, 1);
				else
					calendar.set(CALENDAR_PERIODS[i], 0);
			if (CALENDAR_PERIODS[i] == period)
				zero = true;
		}

		if (!zero)
			throw new IllegalArgumentException("unknown Calendar period: "
					+ period);
		else
			return calendar;
	}

	public static DateRange toDateRange(Calendar date, int period) {
		Calendar cal = (Calendar) date.clone();
		toStartOfPeriod(cal, period);
		java.util.Date startDate = new java.util.Date(cal.getTimeInMillis());
		cal.add(period, 1);
		java.util.Date endDate = new java.util.Date(cal.getTimeInMillis());
		return new DateRange(startDate, endDate);
	}

	public static java.util.Date tomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(5, 1);
		return cal.getTime();
	}

	public static java.util.Date yesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(5, -1);
		return cal.getTime();
	}

	private static Calendar initCalendar(int year, int month, int day,
			int hour, int minute, int second, int millis) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day, hour, minute, second);
		calendar.set(14, millis);
		return calendar;
	}

	public static class DateRange {

		public final java.util.Date startDate;

		public final java.util.Date endDate;

		public DateRange(java.util.Date startDate, java.util.Date endDate) {
			this.startDate = startDate;
			this.endDate = endDate;
		}
	}

	/**
	 * This method generates a string representation of a date/time in the
	 * format you specify on input
	 * 
	 * @param aMask
	 *            the date pattern the string is in
	 * @param strDate
	 *            a string representation of a date
	 * @return a converted Date object
	 * @see java.text.SimpleDateFormat
	 * @throws ParseException
	 *             when String doesn't match the expected format
	 */
	public static Date convertStringToDate(String aMask, String strDate)
			throws ParseException {
		SimpleDateFormat df;
		Date date;
		df = new SimpleDateFormat(aMask);

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}

	/**
	 * This method generates a string representation of a date based on the
	 * System Property 'dateFormat' in the format you specify on input
	 * 
	 * @param aDate
	 *            A date to convert
	 * @return a string representation of the date
	 */
	public static String convertDateToString(Date aDate) {
		return getDateTime(getDatePattern(), aDate);
	}

	public static String getDatePattern() {
		return "yyyy-MM-dd";
	}

	/**
	 * This method generates a string representation of a date's date/time in
	 * the format you specify on input
	 * 
	 * @param aMask
	 *            the date pattern the string is in
	 * @param aDate
	 *            a date object
	 * @return a formatted string representation of the date
	 * 
	 * @see java.text.SimpleDateFormat
	 */
	public static String getDateTime(String aMask, Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (aDate == null) {

		} else {
			df = new SimpleDateFormat(aMask);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

}
